//创建用户相关的小仓库
import { defineStore } from "pinia";
//引入接口
import { reqLogin, reqUserAdd, reqUserAll, reqUserFindById, reqUserPage, reqUserRemove, reqUserUpdate } from "@/api/user";
//引入数据类型
import type { loginFormData, loginResponseData, userAddResponseData, userAllResponseData, userInfoData, userListFormData, userListResponseData, userRemoveResponseData } from "@/api/user/type";
import type { UserState } from "./types/type";
//创建用户小仓库
//User是当前小仓库的唯一id
let useUserStore = defineStore("User",{
    
    //之类使用选择是Api写法
    //存储数据位置
    state:():UserState=>{
        return{
            token:localStorage.getItem("B0445-token") || "",
            user:JSON.parse(localStorage.getItem("B0445-user") as string || '{}'),
            users:[]
        }
    },
    //异步逻辑
    actions:{
        //用户登录方法,引入axync函数会返回一个promise对象
        //所以可以通知组件请求是否成功
        async userLogin(data:loginFormData){
            //登录请求
            let result:loginResponseData = await reqLogin(data);
            //登录成功:获取token,进行存储hi后在任何请求都在headers中进行携带
            //登录失败:获取失败错误信息
            if(result.status == 0){
                //pinia仓库存储token
                //由于pinia存储数据利用的是js对象，所有刷新后会恢复到默认值
                this.token = result.data?.token as string;
                this.user = result.data?.user as userInfoData;
                

                //本地持久化存储
                localStorage.setItem("B0445-token",this.token)
                localStorage.setItem("B0445-user",JSON.stringify(this.user as userInfoData) as string);
                //当前async函数返回一个成功的promise
                return "ok";
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        removeLogout(){
            this.user = null;
            this.token = "";
            localStorage.removeItem("B0445-token");
            localStorage.removeItem("B0445-user");
        },
        //获取所有用户
        async getUserAll(){
            let result:userAllResponseData = await reqUserAll();
            if(result.status == 0){
                this.users = result.data as userInfoData[];
                return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
            
        },
        //分页查询所有用户
        async getUserList(data:userListFormData){
            let result:userListResponseData = await reqUserPage(data);
            if(result.status == 0){
                this.users = result.data?.data as userInfoData[];
                return result.data

            }else{
                return Promise.reject(new Error(result.msg))
            }
            
        },
        //新增用户
        async addUser(data:userInfoData){
            let result:userAddResponseData = await reqUserAdd(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        //更新用户
        async updateUser(data:userInfoData){
            let result:userAddResponseData = await reqUserUpdate(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg));
            }
        },
        //根据id查询用户
        async findById(_id:string){
            let result:userAddResponseData = await reqUserFindById(_id);
            if(result.status == 0){
                const user = result.data as userInfoData;
                return user;
            }else{
                return Promise.reject(new Error(result.msg));
            }
        },
        //删除用户
        async removeUser(_id:string){
            let result:userRemoveResponseData = await reqUserRemove(_id)
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg));
            }
        },
        removeStoreUser(){
            this.user = null;
            this.token = "";
            localStorage.removeItem("B0445-token");
            localStorage.removeItem("B0445-user");
        }
    }
})

//把小仓库暴露出去
export default useUserStore;